 var deminTab = null;
 var zoneTab = null;
 var ERROR = 8;
 var WARNING = 4;
 var INFO = 2;
 var DEBUG = 1;
 
 var traceLevel = INFO;
 
function trace(msgLevel, msg) {
  if (msgLevel>=traceLevel) air.trace(msg);
}

document.gameEventDispatcher = new air.EventDispatcher();

        
function fillZone(i,j, zoneVal, zoneId, nbMaxLigne, nbMaxColonne) {
	var ligneMin = Math.max(i-1,0);
	var ligneMax = Math.min(i+2,nbMaxLigne);
	var colMin = Math.max(j-1,0);
	var colMax = Math.min(j+2,nbMaxColonne);
  trace(DEBUG,"fill zone "+zoneId+" with zoneVal = "+zoneVal+" from "+ligneMin+" -> "+ligneMax+" and from "+colMin+" -> "+colMax); 
  for (var k = ligneMin;k<ligneMax;k++) {
		for (var l = colMin;l<colMax;l++) {
			var currIdZone = zoneTab[k][l].idZone;                  
      var valTab = deminTab[k][l];
      if (valTab == zoneVal) {
				if (currIdZone == 0) {
					zoneTab[k][l].idZone = zoneId;
          fillZone(k,l,zoneVal, zoneId, nbMaxLigne,nbMaxColonne);
        }
			} else if (zoneVal == 0) {
				var znc = zoneTab[k][l].zonesConnexes;
        znc.push(zoneId);
	    }
    }
	}
}
function initTab(nbMaxLigne, nbMaxColonne,nbMaxBomb) {
	deminTab = new Array(nbMaxLigne);
  for (var i = 0;i<nbMaxLigne;i++) {
		deminTab[i] = new Array(nbMaxColonne);
    for (var j = 0;j<nbMaxColonne;j++) {
			deminTab[i][j] = 0;
    }
  } 
	var nbBomb = 0;
  var nbIter = 0;
  while(nbBomb<nbMaxBomb && nbIter < 1000) {
		nbIter++;
    var i = Math.round(Math.random()*(nbMaxLigne-1));
    var j = Math.round(Math.random()*(nbMaxColonne-1));
    if (traceLevel == DEBUG) trace(DEBUG,nbIter+". i = "+i+", j="+j);
    if (deminTab[i][j] == 16) continue;
    trace(DEBUG,"add bomb");
    deminTab[i][j] = 16;
    var ligneMin = Math.max(i-1,0);
    var ligneMax = Math.min(i+2,nbMaxLigne);
    var colMin = Math.max(j-1,0);
    var colMax = Math.min(j+2,nbMaxColonne);
    trace(DEBUG,"update from "+ligneMin+" -> "+ligneMax+" and from "+colMin+" -> "+colMax); 
    for (var k = ligneMin;k<ligneMax;k++) {
			for (var l = colMin;l<colMax;l++) {
				trace(DEBUG," k = "+k+", l = "+l);
        var valTab = deminTab[k][l];
        trace(DEBUG," valTab = "+valTab);
        if (valTab != 16) {
					deminTab[k][l] = valTab + 1;
          trace(DEBUG," udpdate to "+deminTab[k][l]);
        }
      }
    }
    nbBomb++;
  }

  // init zoneTab
  trace(DEBUG," initialisation zoneTab");
  zoneTab = new Array(nbMaxLigne);
  for(var i = 0;i<nbMaxLigne;i++) {
		zoneTab[i] = new Array(nbMaxColonne);
    for(var j = 0;j<nbMaxColonne;j++) {
			var zt = zoneTab[i][j] = new Object();
      zt.idZone = 0;
      zt.zonesConnexes =new Array();
    }     
  }

	// fill zoneTab according to deminTab values
  var curZone = 1;
  for(var i = 0;i<nbMaxLigne;i++) {
		for(var j = 0;j<nbMaxColonne;j++) {
			if (zoneTab[i][j].idZone == 0) {
				fillZone(i,j, deminTab[i][j], curZone, nbMaxLigne, nbMaxColonne);
				curZone++;
      }
    }     
  }               
}
  
function showDeminTab(nbMaxLigne, nbMaxColonne) {
	var divTabJeuxContent;
	divTabJeuxContent = "<"+"table class=\"demineurTab\">";

  for(i = 0;i<nbMaxLigne;i++) {
		divTabJeuxContent += "<"+"tr>";
    for(j = 0;j<nbMaxColonne;j++) {
			var valAffiche = deminTab[i][j];
      divTabJeuxContent += "<"+"td class=\"demineurBox\" boxContent=\"hidden\" ligne=\""+i+"\" colonne=\""+j+"\" bombFlag=\"0\" valCell=\""+valAffiche+"\" boxZone=\""+zoneTab[i][j].idZone+"\"";
      var znc = zoneTab[i][j].zonesConnexes;
      trace(DEBUG,"avant "+ i+","+j+" : "+znc);
      for(k = 0;k<znc.length;k++) {
				trace(DEBUG,"apres");
        divTabJeuxContent += " zc" + znc[k] + "=\"1\"";
      }
      divTabJeuxContent += "><img src=\"images/flag_white.gif\" width=\"50%\" oncontextmenu=\"javascript:return true;\" class=\"bombFlag\" style=\"visibility:hidden\"/></td>";
    }
    divTabJeuxContent += "<"+"/tr>";
  }
  divTabJeuxContent += "<"+"/table>";
  $("#tabJeux").html(divTabJeuxContent);
}

function sendCommand(cmdName, cmdParams) {
	document.gameCmd = cmdName;
	document.gameCmdParams = cmdParams;
	document.gameEventDispatcher.dispatchEvent(new air.Event("GAMEEVT"));
}

document.nbMaxLigne = 9;
document.nbMaxColonne = 9;
document.nbMaxBomb = 10;
document.nbBombWithFlag = 0;
document.initGame = function() {
	var nbMaxLigne = document.nbMaxLigne;
  var nbMaxColonne = document.nbMaxColonne;
  var nbMaxBomb = document.nbMaxBomb;
  document.nbRemainingFlags = document.nbMaxBomb;
  document.nbBombWithFlag = 0;
  $('#youwin').css("visibility","hidden");
  trace(INFO,"initGame l="+nbMaxLigne+" c="+nbMaxColonne+" b="+nbMaxBomb);
  initTab(nbMaxLigne,nbMaxColonne,nbMaxBomb);
  showDeminTab(nbMaxLigne,nbMaxColonne);
  $(".demineurBox").click(function() {
      var selCell = $(this);
      var bombFlag = selCell.attr("bombFlag");
      if (bombFlag == 1) {
        return;
      }
      selCell.attr("boxContent","visible");
      var valCell = selCell.attr("valCell");
      if (valCell == 16) {
	      /*Image("images/bomb.gif").*/
        $(".demineurBox[valCell=16]").attr("boxContent","visible").html('<img src="images/bomb.gif" />');
        sendCommand("looseGame", "");
      } else if (valCell != 0) {
	      selCell.html("<span class=\"demineurBoxValCell"+valCell+"\">"+valCell+"</span>");
      } else {
	      var boxZone = selCell.attr("boxZone");
        $(".demineurBox[@boxZone="+boxZone+"]").each(function() {
	        var zoneCell = $(this);
	        var bombFlag = zoneCell.attr("bombFlag");
	        if (bombFlag == 1) {
	        	return;
	        }
	        zoneCell.attr("boxContent","visible");
	        zoneCell.html("");
				});
				$(".demineurBox[@zc"+boxZone+"=1]").each(function() {
					var zoneCell = $(this);
					var bombFlag = zoneCell.attr("bombFlag");
          if (bombFlag == 1) {
           return;
          }
					zoneCell.attr("boxContent","visible");
					var currBoxZone = zoneCell.attr("boxZone");
					var valZoneCell = zoneCell.attr("valCell");
					zoneCell.html("<span class=\"demineurBoxValCell"+valZoneCell+"\">"+valZoneCell+"</span>");
				});
			}
	});
		$(".demineurBox").mouseout(function() {
		var selCell = $(this);
		var boxContent = selCell.attr("boxContent");
		if (boxContent != "visible") {
      var bombFlag = selCell.attr("bombFlag");
      if (bombFlag == 1) {
        return;
      }
  		selCell.find("img").css("visibility","hidden");
		}
	});
	$(".demineurBox").mouseover(function() {
		var selCell = $(this);
		if (document.nbRemainingFlags <= 0) {
			return;
		}
		var boxContent = selCell.attr("boxContent");
		if (boxContent != "visible") {
			selCell.find("img").css("visibility","visible");
		}
	});
	
	$(".bombFlag").click(function() {
		var bombFlagImg = $(this);
		var selCell = $(this).parent();
		var bombFlag = selCell.attr("bombFlag");
		
    if (bombFlag == 1) {
    	bombFlagImg.attr("src","images/flag_white.gif");
    	selCell.attr("bombFlag","0");
    	document.nbRemainingFlags = document.nbRemainingFlags + 1;
      sendCommand("removeFlag", {nbFlags : document.nbRemainingFlags});
			var valCell = selCell.attr("valCell");
			if (valCell == 16) {
				document.nbBombWithFlag--;
			}      
    	return false;
    } else if (document.nbRemainingFlags > 0) {
    	bombFlagImg.attr("src","images/flag_red.png");
    	selCell.attr("bombFlag","1");
    	document.nbRemainingFlags = document.nbRemainingFlags - 1;
    	sendCommand("addFlag", {nbFlags : document.nbRemainingFlags});
			var valCell = selCell.attr("valCell");
			if (valCell == 16) {
				document.nbBombWithFlag++;
				if (document.nbBombWithFlag == document.nbMaxBomb) {
					sendCommand("winGame", "");
					$('#youwin').css("visibility","visible");
				}
			}
    	return false;
    }
	});
}

$(document).ready(function() {
	/*var menu = null;
	if (air.NativeApplication.supportsMenu) {
		menu = air.NativeApplication.menu;
	}
	if (air.NativeWindow.supportsMenu) {
		menu = nativeWindow.menu = new air.NativeMenu();
	}
	var gameMenuItem = menu.addItem(new air.NativeMenuItem("Jeux"));
	var gameMenu = gameMenuItem.submenu = new air.NativeMenu(); 
	var newMenu = gameMenu.addItem(new air.NativeMenuItem("Nouveau"));
	newMenu.addEventListener(air.Event.SELECT, function(event) {
			initGame();
	});
	var optionMenu = gameMenu.addItem(new air.NativeMenuItem("Options"));
	optionMenu.addEventListener(air.Event.SELECT, function(event) {
		$("#optionsPanel").show();
	});*/
	
});

